# 选课系统代码提示
from user.auth import reg,login
from core.course.course_action import select_course,drop_course,view_course,set_score,show_score

is_login = False
student_id = None

def select_course_api():
    print(is_login, student_id)
    if not is_login:
        # 未登录状态
        print("请先登录！")
        return
    code,course_code = select_course(student_id)
    if code == 501:
        print(f"{course_code}已经选修了，不能重复选！")
    elif code == 502:
        print("课程时间冲突！")
    else:
        print(f"{student_id}选修{course_code}课程成功！")

def login_api():
    ret = login()
    if ret == 401:
        print("账号或密码错误！")
    elif ret == 402:
        print(f"学号不存在，请先注册")
    else:
        global is_login
        global student_id

        is_login = True
        student_id = ret

def reg_api():
    ret, user = reg()
    if ret:
        print(f"学生{user}注册成功！")
    else:
        print(f"学生{user}已经存在！")

def view_course_api():
    if not is_login:
        # 未登录状态
        print("请先登录！")
        return
    view_course(student_id)

def set_score_api():
    student_id = input("请输入学生id:")
    course_code = input("请输入课程code:")
    score = input("请输入课程分数")
    code = set_score(student_id, course_code, score)
    if code == 601:
        print("您未选修过任何课程！")
    elif code == 602:
        print(f"您未选修该课程{course_code}")
    else:
        print("code:::", code)
        print(f"学生{student_id}设置成绩成功！")

def show_score_api():
    if not is_login:
        # 未登录状态
        print("请先登录！")
        return
    code = show_score(student_id)
    if code == 601:
        print("您未选修过任何课程！")


def drop_course_api():
    print(is_login, student_id)
    if not is_login:
        # 未登录状态
        print("请先登录！")
        return
    code,course_code = drop_course(student_id)
    if code == 601:
        print("您未选修过任何课程！")
    elif code == 602:
        print(f"您未选修该课程{course_code}")
    else:
        print(f"学生{student_id}成功退课：{course_code}")

def main():
    while True:
        print("欢迎使用选课系统！")
        print("1. 注册学生")
        print("2. 登录")
        print("3. 选课")
        print("4. 退课")
        print("5. 查看已选课程")
        print("6. 查看成绩")
        print("7. 设置成绩")

        choice = input("请输入您的选项：")

        handler = {
            "1": reg_api,
            "2": login_api,
            "3": select_course_api,
            "4": drop_course_api,
            "5": view_course_api,
            "6": show_score_api,
            "7": set_score_api
        }

        handler[choice]()
